/**
 * LxbRequestData.java
 *
 * @author liujie11
 * 
 * @date 2014-3-20
 * 
 * Copyright 2014 Baidu. All rights reserved. 
 */
package com.japher.hackson.requests;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.Enumeration;
import java.util.Hashtable;

import com.baidu.common.net.HttpRequestData;

/**
 * Subclass of Base HTTP Request Data,
 * 
 * we encrypt and encode all HTTP request's get parameters using RSA algorithm
 * 
 * in this class.Is also maintains final static variable for URL or parameters.
 * 
 * @author liujie11
 * 
 */
public class HacksonReuqestData extends HttpRequestData {
    
    public static final String URL_COMMON_PARAM_API_VER = "apiVer";
    public static final String URL_COMMON_PARAM_MOBILE_TYPE = "mobileType";
    
    public static String BASE_URL = "http://10.46.139.38:8300";
    //  public static String BASE_URL = "http://10.38.95.100/";
    
 // public static String BASE_URL = "http://10.38.128.40:8500/";
    public static String URL_LOGIN = "app/login";
    public static String BASE_PUSH_URL = "http://220.181.75.133:38080/";
    public static String BASE_SECURITY_URL = "http://lxb.baidu.com/";
    
    public static String URL_GET_RECALL_TOKEN = "cb/user/check";
    public static String URL_CALL_LIST = "app/callList";
    public static String URL_CALL_HISTORY_LIST = "app/callHistoryList";
    public static String URL_SAVE_CUSTOM_INFO = "app/saveCustomInfo";
    public static String URL_CALL_LIST_DETAIL = "app/callListDetails";
    public static String URL_FEEDBACK = "app/suggest";
    public static String URL_CHECKVERSION = "app/checkVersion";
    public static String URL_RECALL = "cb/appCallback";
    public static String URL_PUSH_REG = "app/regist";
    public static String URL_SYNCALL = "app/syncCalllist";
    public static String URL_ADS = "app/wordAdvertisement";
    
    
    // public static String URL_PARAM_DISTANCE = "distance";
    // public static String URL_PARAM_POLL_COMPARE_ID = "compareId";
    // public static String URL_PARAM_RECOMMEND_TYPE_ID = "type_id";
    
    private Hashtable<String, String> postParams = new Hashtable<String, String>();
    private Hashtable<String, String> getParams = new Hashtable<String, String>();
    
    private boolean isSecurity = false;
    private String stamp = "";
    
    public boolean isSecurity() {
        return isSecurity;
    }
    
    public void setSecurity(boolean isSecurity) {
        this.isSecurity = isSecurity;
    }
    
    public HacksonReuqestData(String url, boolean isSecurity) {
        this.setUrl(url);
        this.setSecurity(isSecurity);
    }
    
    public HacksonReuqestData(String url) {
        this.setUrl(url);
    }
    
    public HacksonReuqestData() {
    }
    
    public void addPostParam(String key, String value) {
        if (key == null || value == null)
            return;
        postParams.put(key, value);
    }
    
    public void addGetParam(String key, String value) {
        if (key == null)
            return;
        if (value == null) {
            getParams.put(key, "");
        } else {
            getParams.put(key, value);
        }
    }
    
    public String getUrl() {
        if (this.isAppUrl()) {
            
            if (url.indexOf('?') > 0) {
                return url + "&" + lxbParam2String(getParams);
            } else {
                return url + "?" + lxbParam2String(getParams);
            }
            
        } else {
            
            if (getParams.size() > 0) {
                if (url.indexOf('?') > 0) {
                    return url + "&" + params2String(getParams);
                } else {
                    return url + "?" + params2String(getParams);
                }
                
            } else {
                return url;
            }
            
        }
    }
    
    public byte[] getData() {
        try {
            return params2String(postParams).getBytes(encode);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
        return null;
    }
    
    public String toString(boolean post) {
        if (post)
            return this.params2String(postParams);
        else
            return this.params2String(getParams);
    }
    
    public String lxbParam2String(Hashtable<String, String> params) {
        String target = "";
        return target;
    }
    
    public String params2String(Hashtable<String, String> params) {
        // if(paramsString != null) return paramsString;
        if (params != null) {
            String target = null;
            StringBuffer temp = new StringBuffer();
            Enumeration<String> keys = params.keys();
            while (keys.hasMoreElements()) {
                String key = (String) keys.nextElement();
                if (key != null) {
                    String value = (String) params.get(key);
                    temp.append(key);
                    temp.append('=');
                    try {
                        temp.append(URLEncoder.encode(value, encode));
                    } catch (UnsupportedEncodingException e) {
                        temp.append("");
                    }
                    temp.append('&');
                }
                
            }
            if (temp.length() > 1)
                temp.deleteCharAt(temp.length() - 1);
            
            target = temp.toString();
            
            stamp = System.currentTimeMillis() + "";
            
            if (isSecurity) {}
            
            // String destinationId =
            // AppContext.getInstance().getUserInfo().getCityCode();
            // String deviceid = DeviceInfo.getInstance().getDeviceId(
            // AppContext.getInstance().getContext());
            // target = target + "&" + URL_COMMON_PARAM_API_VER + "=" +
            // AppConfig.API_VER_INT;
            // target = target + "&" + URL_COMMON_PARAM_MOBILE_TYPE + "=" +
            // AppConfig.OS_TYPE_ANDROID;
            // target = target + "&" + "deviceId" + "=" + deviceid;
            //
            // if (destinationId != null && !target.contains("&city=")) {
            // target = target + "&city=" + destinationId;
            // }
            
            return target;
        }
        return "";
    }
    
    public Hashtable<String, String> getPostParamHash() {
        return this.postParams;
    }
    
    public Hashtable<String, String> getGetParamHash() {
        return this.getParams;
    }
    
}
